package com.jy.adscm.operation.cg.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.cg.bean.CgBom;
import com.jy.adscm.operation.cg.bean.CgBomAndDate;
import com.jy.adscm.operation.cg.bean.MachineProcess;
import com.jy.adscm.operation.cg.bean.QueryCondition;
import com.jy.adscm.operation.cg.mapper.mysql.CgBomAndDateMapper;
import com.jy.adscm.operation.cg.mapper.mysql.CgBomMapper;
import com.jy.adscm.operation.cg.service.CgBomAndDateService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.operation.cg.service.MachineProcessService;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.utils.AdscmDateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-07-17
 */
@Service
public class CgBomAndDateServiceImpl extends ServiceImpl<CgBomAndDateMapper, CgBomAndDate> implements CgBomAndDateService {
    @Autowired
    CgBomMapper cgBomMapper;
   @Autowired
   MachineProcessService machineProcessService;
    @Override
    public boolean create(QueryCondition queryCondition)throws AdscmSystemException {
     /*   List<MachineProcess> machineProcesses = machineProcessService.selectMac(queryCondition);
        for (MachineProcess machineProcess : machineProcesses) {
            List<CgBom> cgBoms = machineProcess.getCgBoms();

            if (cgBoms!=null &&cgBoms.size()>0){
                for (CgBom cgBom : cgBoms) {
                    if (AdscmBaseUtils.getInstance().isNotEmpty(cgBom,null)){
                        Wrapper<CgBomAndDate> wrapper=new EntityWrapper<>();
                        wrapper.eq("bom",cgBom.getId());
                        wrapper.eq("makeNo",queryCondition.getMakeNo());
                        List<CgBomAndDate> cgBomAndDates = super.selectList(wrapper);
                        if (!(cgBomAndDates!=null&& cgBomAndDates.size()>0)){
                            CgBomAndDate cgBomAndDate =new CgBomAndDate();
                            cgBomAndDate.setBom(cgBom.getId());
                            cgBomAndDate.setArriveDate(AdscmDateUtils.getFormateDate(machineProcess.getStartTime()) );
                            cgBomAndDate.setMakeNo(queryCondition.getMakeNo());
                            super.insert(cgBomAndDate);
                        }
                    }

                }
            }

        }*/
        return false;
    }

    @Override
    public HashMap<String,List<CgBomAndDate>> select(String makeNo) {
        HashMap<String,List<CgBomAndDate>> result=new HashMap<>();
      Wrapper<CgBomAndDate>  wrapper=new EntityWrapper<>();
        wrapper.eq("makeNo",makeNo);
        List<CgBomAndDate> cgBomAndDates = super.selectList(wrapper);
        for (CgBomAndDate cgBomAndDate : cgBomAndDates) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(cgBomAndDate.getBom(),null)){
                CgBom cgBom = cgBomMapper.selectById(cgBomAndDate.getBom());
                cgBomAndDate.setBomName(cgBom.getName());
                String date = AdscmDateUtils.getNowOnlyDateFomat1(cgBomAndDate.getArriveDate());
                List<CgBomAndDate> cgBomAndDatesFromMap = result.get(date);
                if (cgBomAndDatesFromMap!=null&&cgBomAndDatesFromMap.size()>0){
                    cgBomAndDatesFromMap.add(cgBomAndDate);
                }else {
                    List<CgBomAndDate> newCgBomAndDatesToMap=new ArrayList<>();
                    newCgBomAndDatesToMap.add(cgBomAndDate);
                    result.put(date,newCgBomAndDatesToMap);
                }
            }
        }
        return result;
    }
}
